import pytest
import time

from common.NeqReq import NeqReq
from common.DBManager import DBManager

from utils.yaml_utils import read_yaml
from utils.url_utils import full_url
from utils.data_process_utils import extract_args


url = "/login/register"
data_path = "./data/register.yaml"
pre_path = "./data/setup/register.yaml"
aft_path = "./data/teardown/register.yaml"


class Test_register:
    
	@pytest.fixture(scope="class",  autouse=True)
	def class_cycle(self):

		dm = DBManager()   
		pre_data = read_yaml(pre_path)
		dm.insert_data(pre_data['table'], pre_data['fields'], pre_data['data'])
		yield
		aft_data = read_yaml(aft_path)
		dm.delete_data(aft_data['table'], aft_data['field'], aft_data['data'])
		dm.close()	


	@pytest.mark.parametrize("args", read_yaml(data_path))
	def test_1(self,args):
        
		data = extract_args(args, ['username','pwd','append'],['username','password','append'])

		nr = NeqReq()
		result = nr.send_request('POST', full_url(url), data)

		assert result['code'] == args['exp1']
		if result['code'] == 200:
			assert result['body'].get('code') == args['exp2']

